package com.yuandian.management.controller;


import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ConfDataCenterService;
import com.yuandian.management.service.IDataCenterAreaService;
import com.yuandian.management.vo.DataCenterAreaInfo;
import com.yuandian.management.vo.DataCenterInfo;
import com.yuandian.management.vo.DataCenterParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 数据中心
 *
 * @author zhoutian
 * @date 2021/1/5 17:13
 */
@RestController
@RequestMapping("/dataCenter")
@Api(tags = "数据中心")
public class ConfDataCenterController {

    @Autowired
    private ConfDataCenterService service;


	@Autowired
	private IDataCenterAreaService dateCenterAreaService;

    /**
     * 根据条件分页查询数据中心列表
     *
     * @param params 参数
     * @return R
     */
    @PostMapping("/list")
	@ApiOperation(value = "根据条件分页查询数据中心列表", notes = "根据条件分页查询数据中心列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "数据中心名称", paramType = "body", required = false, dataTypeClass = String.class),
	})
    public R list(@RequestBody @ApiIgnore DataCenterParams params) {
        return R.ok(service.queryDataCenterInfos(params));
    }

    /**
     * 根据参数编号获取详细信息
     *
     * @param id
     * @return R
     */
    @GetMapping(value = "/{id}")
	@ApiOperation(value = "根据参数编号获取详细信息", notes = "根据参数编号获取详细信息")
    public R getInfo(@PathVariable("id") Integer id) {
        return R.ok(service.queryDataCenterInfoById(id));
    }

    /**
     * 修改数据中心信息
     *
     * @param params 参数
     * @return R
     */
    @PutMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "数据中心名称", paramType = "body", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "sequence", value = "排序", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = true, dataTypeClass = String.class),
	})
	@ApiOperation(value = "修改数据中心信息", notes = "修改数据中心信息")
    public R edit(@RequestBody @ApiIgnore DataCenterInfo params) {
		boolean flag = service.updateDataCenterInfo(params);
		if (flag) {
			return R.ok("修改成功");
		}
		return R.failed("修改失败");
    }


    /**
     * 新增数据中心信息
     *
     * @param info 参数
     * @return R
     */
    @PostMapping
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "数据中心名称", paramType = "body", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "sequence", value = "排序", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "remarks", value = "备注", paramType = "body", required = true, dataTypeClass = String.class),
	})
	@ApiOperation(value = "新增数据中心信息", notes = "新增数据中心信息")
    public R add(@Validated @RequestBody @ApiIgnore DataCenterInfo info) {
        if (service.checkDataExist(info.getName())) {
            return R.failed("新增数据中心失败，名称已存在");
        }
		boolean flag = service.insertDataCenterInfo(info);
		if (flag) {
			return R.ok("");
		}
		return R.failed("新增失败");
    }

    /**
     * 删除数据中心区域
     *
     * @param ids id集
     * @return R
     */
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Integer[] ids) {
		boolean flag = service.deleteDataByIds(ids);
		if (flag) {
			return R.ok("删除成功");
		}
		return R.failed("删除失败");
    }



	/**
	 * 根据条件查询数据中心区域信息
	 *
	 * @param params 参数
	 * @return Result
	 */
	@GetMapping("/area/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "centerId", value = "数据中心ID", paramType = "query", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false, dataTypeClass = String.class),
	})
	@ApiOperation(value = "查询数据中心区域Id集", notes = "查询数据中心区域Id集")
	public R listArea(@ApiIgnore DataCenterParams params) {
		return R.ok(dateCenterAreaService.selectAreaIdList(params));
	}


	/**
	 * 根据条件分页查询数据中心列表
	 *
	 * @param params 参数
	 * @return Result
	 */
	@GetMapping("/area/getAreaList")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "centerId", value = "数据中心ID", paramType = "query", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "name", value = "名称", paramType = "query", required = false, dataTypeClass = String.class),
			@ApiImplicitParam(name = "mapName", value = "省份拼音", paramType = "query", required = false, dataTypeClass = String.class),
	})
	@ApiOperation(value = "查询区域列表", notes = "查询区域列表")
	public R getAreaList(@ApiIgnore DataCenterParams params) {
		return R.ok(dateCenterAreaService.selectAreaList(params));
	}

	/**
	 * 根据参数编号获取详细信息
	 *
	 * @param id id
	 * @return Result
	 */
	@GetMapping(value = "/area/{id}")
	@ApiOperation(value = "查询数据中心区域详情", notes = "查询数据中心区域详情")
	public R getInfolist(@PathVariable("id") Integer id) {
		return R.ok(dateCenterAreaService.selectDataCenterAreaById(id));
	}

	/**
	 * 修改数据中心信息
	 *
	 * @param params 参数
	 * @return Result
	 */
	@PutMapping("/area")
	@ApiOperation(value = "修改数据中心信息", notes = "修改数据中心信息")
	public R editArea(@RequestBody DataCenterInfo params) {
		return dateCenterAreaService.updateDataCenterArea(params);
	}


	/**
	 * 新增数据中心区域信息
	 *
	 * @param params 如参信息
	 * @return Result
	 */
	@PostMapping("/area")
	@ApiOperation(value = "新增数据中心区域分配", notes = "新增数据中心区域分配")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "centerId", value = "数据中心ID", paramType = "body", required = false, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "areaIds", value = "区域ID", paramType = "body", required = false, dataTypeClass = String.class),
	})
	public R add(@Validated @RequestBody @ApiIgnore DataCenterAreaInfo params) {
		return dateCenterAreaService.insertDataCenterArea(params);
	}


	/**
	 * 删除数据中心区域
	 *
	 * @param ids id集
	 * @return Result
	 */
	@DeleteMapping("/area/{ids}")
	public R removeByIds(@PathVariable("ids") Integer[] ids) {
		return dateCenterAreaService.deleteDataCenterAreaByIds(ids);
	}



}
